package com.daily.videoedit.activity;

import android.Manifest;
import android.os.Bundle;
import android.os.Handler;

import com.daily.videoedit.R;
import com.daily.videoedit.app.App;
import com.daily.videoedit.dao.DaoSharedPreferences;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.xunxi.mimi.ui.base.BaseActivity;

import io.reactivex.functions.Consumer;

/**
 * 作者: liyu on  2017/11/15 0015 14:29
 * 功能描述:
 * 备注:
 */
public class SplashActivity extends BaseActivity {

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState, String tag) {

    }

    @Override
    protected void initView() {
        setStatusBarTintResource(R.color.transparent);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initValue() {
        boolean showAgreeDialog = DaoSharedPreferences.getInstance().getShowAgreeDialog();
        if (showAgreeDialog){

        }else {
            mHandler.postDelayed(mRunnable, 1500);
        }
    }

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            new RxPermissions(SplashActivity.this).request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE).subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean aBoolean) throws Exception {
                    if (aBoolean) {
                        Bundle bundle = getIntent().getExtras();
                        if (bundle != null) {
                            startIntent(MainActivity.class, bundle);
                            finish();
                        } else {
                            startIntentAndFinish(MainActivity.class);
                        }
                    }else {
                        finish();
                        App.getInstance().exit();
                    }
                }
            });
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mHandler != null) {
            mHandler.removeCallbacks(mRunnable);
        }
    }
}
